<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category FileSystemController.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/3/24  21:41
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin\V1;


use App\Rules\ContinuousCharacter;
use App\Rules\Directory\ParentDirectory;
use App\Rules\EmojiChar;
use App\Util\FileSystem;
use Illuminate\Http\Request;

class FileSystemController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  14:47
     */
    public function files(Request $request)
    {
        $directory = $request->get('directory', '') ?? '';
        $search    = $request->get('search', null) ?? null;
        $offset    = $request->get('offset', 0) ?? 0;
        $length    = $request->get('length', 100) ?? 100;
        $offset    = ($offset - 1 < 0 ? 0 : $offset - 1) * $length;

        $fileSystem = new FileSystem($directory);

        $data = $fileSystem->lists($offset, $length, $search)->toArray();

        return $this->responseSuccessData('获取成功', $data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidationException
     * @Depiction 创建文件夹
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  11:23
     */
    public function makeDirectory(Request $request)
    {

        $rules      = [
            'directory' => ['required', 'string', 'between:1,60', new EmojiChar, new ParentDirectory, new ContinuousCharacter],
        ];
        $attributes = [
            'directory' => '文件夹',
        ];
        $messages   = [
            'directory.required' => ':attribute不能为空',
            'directory.string'   => ':attribute必须为字符串',
        ];
        $this->validate($request, $rules, $messages, $attributes);

        try {
            $directory  = $request->post('directory');
            $fileSystem = new FileSystem($directory);
            if ($fileSystem->makeDirectory($directory)) {

                return $this->responseSuccessData('创建成功');
            }
            return $this->responseErrorData('创建失败');
        } catch (\LogicException $exception) {
            return $this->responseErrorData($exception->getMessage());
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidationException
     * @Depiction 删除文件夹
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  14:47
     */
    public function deleteDirectory(Request $request)
    {
        $rules      = [
            'directory' => ['required', 'string', 'between:1,60', new EmojiChar, new ParentDirectory, new ContinuousCharacter],
        ];
        $attributes = [
            'directory' => '文件夹',
        ];
        $messages   = [];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $directory  = $validated['directory'];
        $fileSystem = new FileSystem($directory);
        if ($fileSystem->deleteDirectory($directory)) {

            return $this->responseSuccessData('删除成功');
        }
        return $this->responseErrorData('删除失败');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidationException
     * @Depiction 上传文件
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  16:25
     */
    public function upload(Request $request)
    {
        $rules      = [
            'file'      => ['required', 'file'],
            'directory' => ['required', 'string', 'between:1,60', new EmojiChar, new ParentDirectory, new ContinuousCharacter],
            'name'      => ['nullable', 'min:1', 'max:1023'],
        ];
        $attributes = [
            'file'      => '文件',
            'directory' => '文件夹',
            'name'      => '文件名',
        ];
        $messages   = [];

        $validated = $this->validate($request, $rules, $messages, $attributes);

        $directory  = $validated['directory'];
        $name       = $validated['name'];
        $fileSystem = new FileSystem($directory);

        try {
            if (isset($name)) {
                $path = $fileSystem->putFileAs($request, 'file', $name);
            } else {
                $path = $fileSystem->putFile($request);
            }

            $data = [
                'path'     => $path,
                'realPath' => asset('storage/' . $path),
            ];

            return $this->responseSuccessData('上传成功', $data);


        } catch (\Throwable $exception) {
            return $this->responseErrorData($exception->getMessage());
        }

    }

    /**
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\ValidationException
     * @Depiction 下载文件
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  16:25
     */
    public function download(Request $request)
    {
        $rules      = [
            'file' => ['required', 'string', 'between:2,200'],
        ];
        $attributes = [
            'file' => '文件',
        ];
        $messages   = [];

        $validated = $this->validate($request, $rules, $messages, $attributes);

        $fileSystem = new FileSystem('');

        return $fileSystem->download($validated['file']);

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @Depiction 删除文件
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  16:24
     */
    public function delete(Request $request)
    {
        $paths      = $request->post('paths');
        $fileSystem = new FileSystem('');
        if ($fileSystem->delete($paths)) {

            return $this->responseSuccessData('删除成功');
        }
        return $this->responseErrorData('删除失败');
    }


}
